package com.joker.userExam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joker.userExam.dao.mapper.StudentScoreMapper;
import com.joker.userExam.dao.repo.StudentScoreRepo;
import com.joker.userExam.model.StudentScore;
import com.joker.userExam.service.IUserCenter;
import com.joker.userExam.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 李杰
 * @date 2021/5/8 - 12:22
 */
@Service
public class UserCenterImpl implements IUserCenter {
    @Autowired
    StudentScoreRepo studentScoreRepo;
    @Autowired
    StudentScoreMapper studentScoreMapper;

    @Override
    public ResultUtil querySelfScore(Integer userId) {
        QueryWrapper<StudentScore> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("is_delete",0);
        queryWrapper.orderByDesc("create_time");
        List<StudentScore> list = studentScoreRepo.list(queryWrapper);
        return ResultUtil.ok("success", list);
    }

    @Override
    public ResultUtil deleteScore(StudentScore studentScore) {
        //假删除  将isDelete 置为1
        studentScore.setIsDelete(1);

        boolean res = studentScoreRepo.updateById(studentScore);
        if (res) {
            return ResultUtil.ok("success");
        } else {
            return ResultUtil.error("error");
        }
    }

    @Override
    public ResultUtil getNewScore(Integer userId) {
        return null;
    }

    @Override
    public ResultUtil querySelfScoreByCondition(StudentScore studentScore) {
        QueryWrapper<StudentScore> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", studentScore.getUserId());
        queryWrapper.eq("is_delete",0);
        queryWrapper.orderByDesc("create_time");
        if(studentScore.getType()!=null){
            queryWrapper.eq("type" , studentScore.getType());
        }
        return ResultUtil.ok("success",studentScoreRepo.list(queryWrapper));
    }


}
